package com.hy.frame.mvvm.simple

import com.hy.frame.util.LogUtil
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.net.ssl.SSLSession

object NetClient {

    fun getOkHttpClient(): OkHttpClient {
        return OkHttpClient.Builder().apply {
            //设置超时
            connectTimeout(10, TimeUnit.SECONDS)
            readTimeout(5, TimeUnit.SECONDS)
            writeTimeout(5, TimeUnit.SECONDS)
            //日志
            addInterceptor(HttpLoggingInterceptor { message -> LogUtil.d("OkHttp", message) }.apply { setLevel(HttpLoggingInterceptor.Level.BODY) })
            //忽略host验证
            hostnameVerifier { _: String?, _: SSLSession? -> true }
        }.build()
    }

    fun getSimpleService(): SimpleService {
        val retrofit = Retrofit.Builder()
            .baseUrl("http://127.0.0.1/")
//                .addConverterFactory(FastJsonConverterFactory.create())
//                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .client(getOkHttpClient())
            .build()
        return retrofit.create(SimpleService::class.java)
    }
}